<?

include_once ("Server.php");
include_once ("config.php");

class PlayerRepo extends BaseRepo{

	function getPlayersForTeam($teamID)
	{
		$con = connect();
		$q = "SELECT p.id,p.first_name,p.last_name,p.nfl_team_id,p.position,p.rank, ";
		$q = $q."nt.location,nt.name,nt.short_name,nt.bye_week,s.pick FROM player p ";
		$q = $q."JOIN selection s ON s.player_id = p.id AND s.team_id = ".$teamID." ";
		$q = $q."JOIN nfl_team nt ON nt.id = p.nfl_team_id ";
		$q = $q."ORDER BY s.pick ASC";
		$playerRows = query($q, $con);
		if (mysql_num_rows($playerRows) > 0)
		{
			$retval = array();
			while ($row = mysql_fetch_array($playerRows))
			{
				$t = new NFLTeam($row['nfl_team_id'],
						   $row['location'],
						   $row['name'],
						   $row['short_name'],
						   $row['bye_week']);
						   
				$p = new Player($row['id'], 
							$row['first_name'], 
							$row['last_name'],
							$row['position'],
							$row['rank'],
							$t);
				$p->pick = $row['pick'];
				$retVal[] = $p;
			}
		}
		disconnect($con);
		return $retVal;
	}

	public function getAllFreeAgentsInDraft($draftID)
	{
		$q = "WHERE id NOT IN(";
			$q = $q."SELECT playerID FROM selection ";
			$q = $q."WHERE draftID=".$draftID;
		$q = $q.") ORDER BY rank ASC";
		$playerRows = query($q, $con);
		if (mysql_num_rows($playerRows) > 0)
		{
			$retval = array();
			while ($row = mysql_fetch_array($playerRows))
			{
				if (!$row['isSelected'])
				{
					$t = new NFLTeam($row['nfl_team_id'], 
									 $row['location'], 
									 $row['name'],
									 $row['short_name'],
									 $row['bye_week']);
									 
					$p = new Player($row['id'], 
								$row['first_name'], 
								$row['last_name'],
								$row['position'],
								$row['rank'], $t);
					$retVal[] = $p;
				}
			}
		}
		else 
		{
			$retVal = false;
		}
	  
		disconnect($con);
		return $retVal;
	}
}
?>